#include "widget.h"
#include "ui_widget.h"
#include <QFile>
#include <QDebug>
#include <QLineEdit>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    char filename[26][100] = {"E://英汉词典a.txt","E://英汉词典b.txt","E://英汉词典c.txt","E://英汉词典d.txt","E://英汉词典e.txt",
                                          "E://英汉词典f.txt","E://英汉词典g.txt","E://英汉词典h.txt","E://英汉词典i.txt","E://英汉词典j.txt",
                                          "E://英汉词典k.txt","E://英汉词典l.txt","E://英汉词典m.txt","E://英汉词典n.txt","E://英汉词典o.txt",
                                          "E://英汉词典p.txt","E://英汉词典q.txt","E://英汉词典r.txt","E://英汉词典s.txt","E://英汉词典t.txt",
                                          "E://英汉词典u.txt","E://英汉词典v.txt","E://英汉词典w.txt","E://英汉词典x.txt","E://英汉词典y.txt",
                                          "E://英汉词典z.txt"
                              };
    int i;
    int j;
    int k;
    int count = 0;

    word_length[0] = 0;
    for(k = 0; k < 26; k++){
        QFile file(filename[k]);
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
            qDebug()<<"Can't open the file!"<<endl;
            exit(1);
        }
        QTextStream in(&file);
        while(!in.atEnd()) {
            QString buffer = in.readLine();
            QString eng;
            for(i=0; buffer[i]!='\0'; i++) {
                if(buffer[i] == ' ') {
                    for(j=0; j<i; j++) {
                        eng[j] = buffer[j];
                    }
                    break;
                }
            }
            word[count] = eng.toUtf8().data();
            mean[count] = buffer.toUtf8().data();
            count++;

        }
        word_length[k+1] = count;
        file.close();
    }
    length = count;

    connect(ui->pushButton_1,&QPushButton::clicked,this,&Widget::WordFind);
    connect(ui->pushButton_2,&QPushButton::clicked,this,&Widget::WordInsert);
    connect(ui->pushButton_3,&QPushButton::clicked,this,&Widget::WordDelete);

}

//二分查找（前提：折半查找要求线性表必须采用顺序存储结构）
//三个参数分别是：要查找的数，数组中要查找的范围，
//返回值是数组的下标
//low是最左边的元素的边缘，high是最右边的元素的边缘
int Widget::HalfSearch(QString n,int low,int high) {
    int middle = (low + high) / 2;//中间元素的下标
    if(low > high) {//递归搜索的第一个出口
        return -1;//未找到这个单词，返回-1；
    }
    //开始搜索
    if (word[middle] == n) {
        return middle;//返回的是找到元素的下标
    } else if (word[middle] < n) {
        low = middle + 1;//查找范围变化，中间值以及所有比中间值小的元素都不考虑
        return HalfSearch(n,low,high);//继续递归
    } else if (word[middle] > n) {
        high = middle - 1;//查找范围变化，中间值以及比中间值大的元素都不考虑
        return HalfSearch(n,low,high);//继续递归
    }
}

void Widget::WordFind() {
    QString w;
    w = ui->lineEdit->text();
    if(HalfSearch(w,0,length) == -1){
        ui->lineEdit_3->setText("未找到该单词");
        ui->lineEdit_2->setText("");
    }else{
        ui->lineEdit_2->setText(mean[HalfSearch(w,0,length)]);
        ui->lineEdit_3->setText("查找成功");
    }
}

void Widget::FileUpdate(int number) {
    int i;
    char filename[26][100] = {"E://英汉词典a.txt","E://英汉词典b.txt","E://英汉词典c.txt","E://英汉词典d.txt","E://英汉词典e.txt",
                                      "E://英汉词典f.txt","E://英汉词典g.txt","E://英汉词典h.txt","E://英汉词典i.txt","E://英汉词典j.txt",
                                      "E://英汉词典k.txt","E://英汉词典l.txt","E://英汉词典m.txt","E://英汉词典n.txt","E://英汉词典o.txt",
                                      "E://英汉词典p.txt","E://英汉词典q.txt","E://英汉词典r.txt","E://英汉词典s.txt","E://英汉词典t.txt",
                                      "E://英汉词典u.txt","E://英汉词典v.txt","E://英汉词典w.txt","E://英汉词典x.txt","E://英汉词典y.txt",
                                      "E://英汉词典z.txt"
                                     };
    QFile file(filename[number]);
    if(!file.open(QIODevice::ReadWrite | QIODevice::Text)) {
            qDebug()<<"Can't open the file!"<<endl;
            exit(1);
    }
    QTextStream stream(&file);
    for(i = word_length[number]; i < word_length[number+1]; i++) {

        if(i == word_length[number+1]-1){
            stream << mean[i];
        }else{
            stream << mean[i] << "\n";
        }
    }
    file.close();
}

void Widget::WordInsert() {
    int i;
    int j;
    QString w1 = ui->lineEdit->text();
    QString w2 = ui->lineEdit_2->text();
    if(HalfSearch(w1,0,length) != -1) {
        ui->lineEdit_3->setText("该单词已经存在了哦，请勿重复添加");
    } else {
        if(ui->lineEdit_2->text()==NULL){
            ui->lineEdit_2->setText("请在该处输入该单词的意思");
            return;
        }

        for(i=0; i<length; i++) {
            if (word[i] > w1) {
                for(j = length; j > i; j--) {//整体后移一位
                    word[j] = word[j-1];
                    mean[j] = mean[j-1];
                }
                word[i] = w1;
                mean[i] = w1 + "   " + w2;
                length++;//单词总长度加一
                ui->lineEdit_3->setText("添加成功");
                return;
            }
        }
        word[length] = w1;
        mean[length] = w1 +  "   " + w2;
        length++;
        ui->lineEdit_3->setText("添加成功");
    }
}

void Widget::WordDelete() {
    int temp;
    int j;
    QString w;
    if(ui->lineEdit->text()==NULL){
        ui->lineEdit->setText("请在该处输入单词");
        return;
    }
    w = ui->lineEdit->text();

    temp = HalfSearch(w,0,length);
    if(temp == -1) {
        ui->lineEdit_3->setText("并没有找到您想删除的单词哦，请确认输入信息是否有误~");
        return;
    }
    for(j = temp; j < length-1; j++) {//将所有单词和解释前移一位
        word[j] = word[j+1];
        mean[j] = mean[j+1];
    }
    length--;//单词总长度减一
    ui->lineEdit_3->setText("删除成功");
}

Widget::~Widget()
{
    delete ui;
}

